package com.ruiben.mall.controller;


import com.ruiben.mall.LoginRequired;
import com.ruiben.mall.impl.CartServiceImpl;
import com.ruiben.mall.pojo.UserCart;
import com.ruiben.mall.utils.CommonJSONResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

import static com.ruiben.mall.config.Config.BASE_URL;
import static com.ruiben.mall.config.ErrorConfig.*;

@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartServiceImpl cartService;

    /**
     * 把商品放入到购物车里
     * @param cart
     * @return
     */
    @PostMapping("/addProduct")
    public CommonJSONResult addProductToCart(UserCart cart){
        if (StringUtils.isBlank(cart.getProductid())){
            return CommonJSONResult.errorMsg("商品id不能为空");
        }

        if (StringUtils.isBlank(cart.getUserid())){
            return CommonJSONResult.errorMsg("用户id不能为空");
        }

        if (cart.getProductnum()==null){
            return CommonJSONResult.errorMsg("数量不能为空");
        }

        // 判断购物车是否有同类型和属性的商品，如果有，增加数量，没有则增加购物车商品
        List<UserCart> cartList = cartService.isExistProductForCart(cart.getProductid());
        if (cartList!=null && cartList.size()>0){
            for (UserCart userCart:cartList) {
                if (cart.getProductid().equals(userCart.getProductid())){
                    if (!StringUtils.isBlank(userCart.getProductAttrId()) && !StringUtils.isBlank(cart.getProductAttrId())){
                        if (cart.getProductAttrId().equals(userCart.getProductAttrId())){
                            userCart.setProductnum(userCart.getProductnum() + cart.getProductnum());
                            userCart.setProductPrice(userCart.getProductPrice() + cart.getProductPrice());
                            boolean b = cartService.updateCart(userCart);
                            if (b){
                                return CommonJSONResult.ok();
                            } else {
                                return CommonJSONResult.errorMsg("加入购物车失败");
                            }
                        }
                    } else if (StringUtils.isBlank(userCart.getProductAttrId()) && StringUtils.isBlank(cart.getProductAttrId())) {
                        userCart.setProductnum(userCart.getProductnum() + cart.getProductnum());
                        userCart.setProductPrice(userCart.getProductPrice() + cart.getProductPrice());
                        boolean b = cartService.updateCart(userCart);
                        if (b){
                            return CommonJSONResult.ok();
                        } else {
                            return CommonJSONResult.errorMsg("加入购物车失败");
                        }
                    }

                }

            }
        }

        boolean b = cartService.addProductToCart(cart);
        if (b){
            return CommonJSONResult.ok();
        } else {
            return CommonJSONResult.errorMsg("放入购物车失败");
        }
    }


    /**
     * 把商品放入到购物车里
     * @return
     */
    @PostMapping("phone/addProduct")
    @LoginRequired
    public CommonJSONResult addProductToCart(String userid, String productid, Integer productnum, BigDecimal productPrice,String productAttrName,String productAttrId){

        if (StringUtils.isBlank(productid)){
            return CommonJSONResult.errorMsg("商品id不能为空");
        }

        if (productnum == null){
            return CommonJSONResult.errorMsg("数量不能为空");
        }

        if (StringUtils.isBlank(userid)){
            return CommonJSONResult.errorMsg("用户id不能为空");
        }
        if (productPrice == null){
            return CommonJSONResult.errorMsg("单价不能为空");
        }


        // 判断购物车是否有同类型和属性的商品，如果有，增加数量，没有则增加购物车商品
        List<UserCart> cartList = cartService.isExistProductForCart(productid);
        if (cartList!=null && cartList.size()>0){
            for (UserCart userCart:cartList) {
                if (productid.equals(userCart.getProductid())){
                    if (!StringUtils.isBlank(userCart.getProductAttrId()) && !StringUtils.isBlank(productAttrId)){
                        if (productAttrId.equals(userCart.getProductAttrId())){
                            userCart.setProductnum(userCart.getProductnum() + productnum);
                            userCart.setProductPrice((new BigDecimal(userCart.getProductPrice()).add(productPrice)).longValue());
                            boolean b = cartService.updateCart(userCart);
                            if (b){
                                return CommonJSONResult.ok();
                            } else {
                                return CommonJSONResult.errorMsg("加入购物车失败");
                            }
                        }
                    } else if (StringUtils.isBlank(userCart.getProductAttrId()) && StringUtils.isBlank(productAttrId)) {
                        userCart.setProductnum(userCart.getProductnum() + productnum);
                        userCart.setProductPrice((new BigDecimal(userCart.getProductPrice()).add(productPrice)).longValue());
                        boolean b = cartService.updateCart(userCart);
                        if (b){
                            return CommonJSONResult.ok();
                        } else {
                            return CommonJSONResult.errorMsg("加入购物车失败");
                        }
                    }

                }

            }
        }

        UserCart userCart = new UserCart();
        userCart.setUserid(userid);
        userCart.setProductAttrId(productAttrId);
        userCart.setProductAttrName(productAttrName);
        userCart.setProductid(productid);
        userCart.setProductnum(productnum);
        userCart.setProductPrice(productPrice.longValue());

        boolean b = cartService.addProductToCart(userCart);
        if (b){
            return CommonJSONResult.ok();
        } else {
            return CommonJSONResult.errorMsg("放入购物车失败");
        }
    }

    /**
     * 从购物车删除商品
     * @param ids
     * @return
     */
    @LoginRequired
    @PostMapping("delProduct")
    public CommonJSONResult delProductForCart(String[] ids){

        if (ids == null || ids.length == 0){
            return CommonJSONResult.build(ID_NOT_EMPTY_CODE,ID_NOT_EMPTY_MSG,null);
        }

        boolean b = cartService.delProductForCart(ids);
        if (b){
            return CommonJSONResult.ok();
        } else {
            return CommonJSONResult.errorMsg("删除失败");
        }
    }

    @PostMapping("getCartList")
    @LoginRequired
    public CommonJSONResult getCartList(String userId){
        List<UserCart> cartList = cartService.getCartList(userId);
        for (UserCart cart:cartList){
            cart.getShopProduct().setImagePath(BASE_URL + cart.getShopProduct().getImagePath());
        }
        return CommonJSONResult.ok(cartList);
    }


    @PostMapping("updateCartGoodsNum")
    @LoginRequired
    public CommonJSONResult updateCartGoodsNum (String id,Integer num) {
        if (StringUtils.isBlank(id)){
            return CommonJSONResult.errorMsg(ID_NOT_EMPTY_MSG);
        }

        if (num==null){
            num = 1;
        }
        boolean b = cartService.updateNumForCart(id,num);
        if (b){
            return CommonJSONResult.ok();
        }

        return CommonJSONResult.errorMsg("更新失败");
    }
}
